<?php 
session_start();
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");


class M_movie extends DB{
    private $dbConn;
    private $totalpage="0";
    private $rowCount="0";
    
    function M_movie(){
        parent::__construct();
    }
    
    function getRowCount(){
        return $this->rowCount;
    }
    
    function getTotalPage(){
        return $this->totalpage;
    }
    function getTotalSize(){
		$sql = "SELECT  IFNULL(sum( f.filesize_GB ) ,0) totalSize ";
		  $sql .= " FROM tb_files2 f ";
			  if (isset($_SESSION['userid'])){
			  $sql .= "   JOIN tb_my_list l on l.mid=f.id
							JOIN tb_users u on u.ID = l.uid and u.ID=".$_SESSION['userid'];
			}else{
			  $sql .= "  JOIN tb_tmp_list l on l.mid=f.id and session_id='".$_SESSION['sess_id']."'";
			}
			$total=0;
			$result = $this->ExecuteData($sql);
			if ($result->RecordCount() > 0) {
				$row=$result->FetchRow();
				$total= $row['totalSize'];
			}else{
				$total= 0;
			}
			return $total;
			
	}
    function getAllVideoByName($name, $pageStart=0,$list_page=10){
         $sql = "SELECT f.id, f.directoryname , f.filesize_GB, f.fileType ";
        
        if (isset($_SESSION['userid'])){
            $sql .= ", CASE WHEN u.ID IS NULL THEN '' ELSE 'Checked' END value ";
        }else{
             $sql .= ", CASE WHEN l.mid IS NULL THEN '' ELSE 'Checked' END value ";
        }
        
        $sql .= " FROM tb_files2 f ";
        
        if (isset($_SESSION['userid'])){
          $sql .= "  LEFT JOIN tb_my_list l on l.mid=f.id
                     LEFT JOIN tb_users u on u.ID = l.uid and u.ID=".$_SESSION['userid'];
        }else{
          $sql .= "  LEFT JOIN tb_tmp_list l on l.mid=f.id and session_id='".$_SESSION['sess_id']."'";
        }
        
        if ($name!=''){
            $sql.=" WHERE directoryname like '%".$name. "%'";
            
        } 
        $sql .= "  ORDER BY  directoryname asc ";
       
        $result = $this->ExecuteData($sql);
        //Get all rows
        $rows =$result->RecordCount();
        
        
        //Page limit
        $sql.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
        
        $result = $this->ExecuteData($sql);
        
        $totalpage=1;
        if ($rows>0){
            $rt = $rows%$list_page;
            
            if($rt!=0) {
                $totalpage = floor($rows/$list_page)+1;
            }
            else {
                $totalpage = floor($rows/$list_page);
            }
        }
        
        $this->rowCount=$rows;
        $this->totalpage=$totalpage;    
        
        return $result;
        
        
    }
    
    function InsertOrder($name,$addr,$hdd, $uid , $totalSize){
        $m_list = $this->orderMyList($uid);
        $order=date("Ymdhis")  + rand(0,9) ;
        $sql = " INSERT INTO tb_orders (order_id, user_id ,totalsize, CopyCost, movie_list, status , sentto, sentaddr, adddate)";
        $sql .= " values ('".$order."',".$uid." , ".$totalSize.",".$hdd.",'".$m_list."',0,'".$name."','".$addr."', UTC_TIMESTAMP()  )";
        
        $result = $this->ExecuteData($sql);
        
        $this->updateCounter($m_list);
        $this->removeOrder($uid);
        
        return true;
    }
    function updateCounter($m_list){
        
        $sql =" Update tb_orders set counter = counter+1 where id IN (".$m_list.") ";
        $result = $this->ExecuteData($sql);
        
    }
    function removeOrder($uid){
        $sql = " DELETE FROM tb_my_list WHERE uid = '".$uid."'  "; 
        $result = $this->ExecuteData($sql);
    }
    
    function orderMyList($uid){
        $sql = "SELECT  mid FROM  tb_my_list  WHERE uid = '".$uid."'  "; 
        $result = $this->ExecuteData($sql);
        
         while ($row = $result->FetchRow()) {
            $my_item .= $row["mid"].",";
         }
         
        return $my_item."0";
    }   
     
    function getMyList($order='default' ,$type='asc'){
         $sql=" SELECT f.filetype, f.directoryname, f.filesize_GB, f.id , f.Category , m.uid
                FROM tb_my_list m ";
         $sql.=" JOIN tb_files2 f on f.id=m.mid ";
         $sql.=" WHERE uid=".$_SESSION['userid'];                
         
         if($order !='default'){
            $sql .= " order by " . $order . " " . $type;    
         }else{
             $sql .= " order by directoryname asc";
         }
        
        $result = $this->ExecuteData($sql);
        return $result;
    }
    function getAllVideo($name='', $pageStart=0,$list_page=10){
        
        $sql = "SELECT f.id, f.directoryname , f.filesize_GB, f.fileType ";
        
        if (isset($_SESSION['userid'])){
            $sql .= ", CASE WHEN u.ID IS NULL THEN '' ELSE 'Checked' END value   ";
        }else{
             $sql .= ", CASE WHEN l.mid IS NULL THEN '' ELSE 'Checked' END value ";
        }
        
        $sql .= " FROM tb_files2 f ";
        
        if (isset($_SESSION['userid'])){
          $sql .= "  LEFT JOIN tb_my_list l on l.mid=f.id
                     LEFT JOIN tb_users u on u.ID = l.uid and u.ID=".$_SESSION['userid'];
        }else{
             $sql .= "  LEFT JOIN tb_tmp_list l on l.mid=f.id and session_id='".$_SESSION['sess_id']."'";    
            
        }
        if ($name!=''){
            
            if (strlen($name)==1){
                
                $sql.=" WHERE left(f.directoryname,1)='".$name. "'";
            }else if ( ($name)=="All") {
                 $sql.=" ";
                
            }else if ( strlen($name) > 2) {
                 $sql.=" WHERE f.Category ='".$name. "'";
                
            }
        } 
        $sql .= " ORDER BY  f.directoryname asc ";
        
        $result = $this->ExecuteData($sql);
        //Get all rows
        $rows =$result->RecordCount();
        
        
        //Page limit
        $sql.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
        
        $result = $this->ExecuteData($sql);
        
        $totalpage=1;
        if ($rows>0){
            $rt = $rows%$list_page;
            
            if($rt!=0) {
                $totalpage = floor($rows/$list_page)+1;
            }
            else {
                $totalpage = floor($rows/$list_page);
            }
        }
        
        $this->rowCount=$rows;
        $this->totalpage=$totalpage;    
        
        return $result;
        
    }
    
    function transferdata($uid, $sessid){
        
       $sql =" Insert into tb_my_list (uid, mid, filesize,addDate)";
       $sql.=" Select ".$uid.", mid, filesize,addDate from tb_tmp_list where session_id='".$sessid."'";
       $result = $this->ExecuteData( $sql );
       
       $delSql=" Delete from tb_tmp_list where session_id='".$sessid."'";
       $result = $this->ExecuteData( $delSql );
    }
    function login($u, $p){
        $sql = "SELECT * FROM tb_users Where username='".$u."' ";
        $sql .=" And password='".$p."'";
        $result = $this->ExecuteData( $sql );
        
        if ($result->RecordCount())
            return $result;
        else 
           return false;
        
    }
    function removeCart($uid, $mid,$type='1'){
        //type=0 : temp
         if ($type==1){
             $sql = " DELETE FROM tb_my_list WHERE uid =".$uid." AND mid='".$mid."'";
         }else{
             $sql = " DELETE FROM tb_tmp_list WHERE session_id ='".$uid."' AND mid='".$mid."'"; 
         }
        $result = $this->ExecuteData( $sql );
        return $result;
    }
    function AddCart($uid, $mid, $size,$type='1'){
        //Type=0: tmp
        
        if ($type==1){
            $sql = " INSERT INTO tb_my_list (uid, mid , filesize , addDate )";
            $sql.=" values (".$uid.",'".$mid."',".$size.", UTC_TIMESTAMP() )";
        }
        else{
            $sql = " INSERT INTO tb_tmp_list (session_id, mid , filesize , addDate )";
            $sql.=" values ('".$uid."','".$mid."',".$size.", UTC_TIMESTAMP() )";
        }
        $result = $this->ExecuteData( $sql );
        return $result;
    }
    function CheckUserName($user){
        $sql = "SELECT * FROM tb_users Where username='".$user."' ";
        $result = $this->ExecuteData( $sql );
        $rows =$result->RecordCount();
        
        return $rows;
    }
    function AddUser($user,$pwd, $email){
        
        $sql = " INSERT INTO tb_users (username, password, email , Enable, CreatedDate )";
        $sql .= " values ('".$user."','".$pwd."','".$email."', 1 , UTC_TIMESTAMP()  )";
        
        $result = $this->ExecuteData( $sql );
        
        return $result;
        
    }
}

?>